<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
class GoodsListController extends CommonController{
	private $model;
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__init();
		//实例化GoodsList模型
		$this->model = new \Common\Model\GoodsList;
	}
	//展示货品
	public function index(){
		//获得ID
		$goods_gid = Q('get.goods_gid',0,'intval');
		//统计总条数
		$total = $this->model->count();
		//分页类使用
		$page = Page::row(10)->make($total);
		View::with('page',$page);
		//获得所有的货品 get()是查询所有的数据
		$data = $this->model->where("goods_gid={$goods_gid}")->limit(Page::limit())->orderBy('glid','DESC')->get();
		foreach($data as $k=>$name){
			$data[$k]['combine'] = explode('|', $data[$k]['combine']);
		}
		//p($data);
		
		
		//
		$GAttrModel = new \Common\Model\GoodsAttr;
		$GAttrData = $GAttrModel->join('typeattr','taid','=','typeattr_taid')->where("goods_gid={$goods_gid}")->get();
		//p($GAttrData);
		foreach($GAttrData as $k=>$v){
			if($v['taname'] == '颜色'){
				$color[] = $v;
			}
			if($v['taname'] == '尺码'){
				$size[] = $v;
			}
		}
		//p($color);
		//p($size);
		//分配数据
		View::with('color',$color);
		View::with('size',$size);
		View::with('GAttrData',$GAttrData);
		
		
		
		//分配数据
		View::with('data',$data);
		//载入模板
		View::make();
	}
	//添加货品
	public function add(){
		//p(Q('post.'));exit;
		if(IS_POST){
			$GListModel = new \Common\Model\GoodsList;
			$GListData = $GListModel->get();
			$data = array(
				'combine' => Q('post.size').'|'.Q('post.color'),
				'number' => Q('post.number'),
				'inventory' => Q('post.inventory'),
				'is_default' => Q('post.is_default'),
				'goods_gid' => Q('post.goods_gid')
			);
			//与数据库对比
			foreach($GListData as $k=>$name){
				//p($GListData[$k]['combine']);
				if($data['combine'] == $GListData[$k]['combine']){
					//$this->error = $GListModel->getError();
					View::error('信息重复',U('GoodsList/index',array('goods_gid'=>Q('post.goods_gid'))));
					return false;
				}
			}
			
			if(!$GListModel->add($data)){
				$this->error = $GListModel->getError();
				return false;
			}
			View::success('添加成功',U('GoodsList/index',array('goods_gid'=>Q('post.goods_gid'))));
		}
	}
} 
	
?>